package usw.framework.minidao.spring.map;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 提供默认小写作为Key的Map
 * 
 *
 */
@SuppressWarnings("rawtypes")
public class MiniDaoLinkedMap extends LinkedHashMap<String, Object> {

	private static final long serialVersionUID = 1L;

	private final Locale locale;

	public MiniDaoLinkedMap() {
		this(((Locale) (null)));
	}

	public MiniDaoLinkedMap(int initialCapacity) {
		this(initialCapacity, null);
	}

	public MiniDaoLinkedMap(int initialCapacity, Locale locale) {
		super(initialCapacity);
		this.locale = (locale == null) ? Locale.getDefault() : locale;
	}

	public MiniDaoLinkedMap(Locale locale) {
		this.locale = locale == null ? Locale.getDefault() : locale;
	}

	public void clear() {
		super.clear();
	}

	/**
	 *
	 * @param key
	 * @return
	 */
	public boolean containsKey(Object key) {

		return (key instanceof String)
				&& super.containsKey(convertKey((String) key));
	}

	protected String convertKey(String key) {
		return key.toLowerCase(locale);
	}

	/**
	 *
	 * @param key
	 * @return
	 */
	public Object get(Object key) {

		if 	(key instanceof String)
			return 	super.get(convertKey((String) key));
		else
			return 	null;
	}

	/**
	 *
	 * @param key
	 * @param value
	 * @return
	 */
	public Object put(String key, Object value) {
		return super.put(convertKey(key), value);
	}

	/**
	 *
	 * @param map
	 */
	public void putAll(Map map) {

		if (map.isEmpty())
			return;

		Map.Entry entry;
		for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); put(
				convertKey((String) entry.getKey()), entry.getValue()))
			entry = (Map.Entry) iterator.next();

	}

	/**
	 *
	 * @param key
	 * @return
	 */
	public Object remove(Object key) {

		if 	(key instanceof String)
			return 	super.remove(convertKey((String) key));
		else
			return 	null;
	}
}
